#!/bin/sh

# Slackware build script for vertex-maia-themes

# Copyright 2020 Isaac Yu <isaacyu1@isaacyu1.com>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# References (Manjaro PKGBUILD):
# github.com/manjaro/packages-community/blob/1cdf193952813754edd68463c652e094027ac650/vertex-maia-themes/PKGBUILD
# Patch:
# github.com/manjaro/packages-community/blob/1cdf193952813754edd68463c652e094027ac650/vertex-maia-themes/whiskermenu-rabideau.patch

PRGNAM=vertex-maia-themes
VERSION=${VERSION:-20170128}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

ARCH=noarch

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

SRCNAM=vertex-theme
SRCNAM2=vertex-manjaro
MANJARO_COMMIT=084cd9576d280e306c00d58528375ed9743fa6fb

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
rm -rf $SRCNAM2-$MANJARO_COMMIT
tar xvf $CWD/$SRCNAM2-$MANJARO_COMMIT.tar.gz
cd $SRCNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

./autogen.sh --prefix=/usr
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux
make
make install DESTDIR=$PKG

##
## Manjaro theme modifications
##
cd extra
for name in "Chrome" "Firefox" "Vertex-Plank" "Vertex_alt_metacity"; do
  find "$name" -type f -not -name *~ -exec install -Dm644 '{}' "$PKG/usr/share/themes/{}" \;
done
cd ..

echo "This next bit might take a little while..."

cd "$PKG/usr/share/themes"
rm -r Chrome Firefox/Vertex-Light Vertex{-Plank,_alt_metacity}
mv Vertex Vertex-Maia
mv Vertex-Dark Vertex-Maia-Dark
mv Firefox/Vertex Firefox/Vertex-Maia
mv Firefox/Vertex-Dark Firefox/Vertex-Maia-Dark

## adjust theme names, default xcursor/icons and authors
find . -type f -exec sed -i \
	"s/Vertex/Vertex-Maia/g;s/DMZ-Black/Maia/g;s/gnome/Vertex-Maia/g;s/horst3180/horst3180\/korrode\/oberon\/ManjaroCinnamonFan/g" {} \;

## Shift the hightlight-hue of all image files from blue to maia-greenish
find . ! -type l -name "*.png" -exec mogrify -modulate 100,100,71 {} \;
find . ! -type l -name "*.svg" -exec mogrify -background none -modulate 100,100,71 {} \;

## Adjust color values
find . ! -type l -name "*.css" -exec sed -i \
	"s/0553ea/1ceab6/g;\
	s/0d9eff/0dff9e/g;\
	s/0e5ffa/16a085/g;\
	s/122237/123722/g;\
	s/171819/171918/g;\
	s/1793e5/16a085/g;\
	s/1c1d1e/1c1e1d/g;\
	s/1E1E1F/1E1F1E/g;\
	s/232324/232423/g;\
	s/244672/247246/g;\
	s/276ffa/16a085/g;\
	s/282829/282928/g;\
	s/284d7d/287d4d/g;\
	s/29292b/292b29/g;\
	s/2a5285/2a8552/g;\
	s/2b2b2c/2b2c2b/g;\
	s/2b2b2d/2b2d2b/g;\
	s/2b2c2e/2b2e2c/g;\
	s/2b5489/2b8954/g;\
	s/2c578d/2c8d57/g;\
	s/2c73fb/16a085/g;\
	s/2e3436/2e3634/g;\
	s/313132/313231/g;\
	s/3294d5/32d594/g;\
	s/3176fb/16a085/g;\
	s/343436/343634/g;\
	s/353638/353836/g;\
	s/37383A/373A38/g;\
	s/38393b/383b39/g;\
	s/393A3B/393B3A/g;\
	s/39689f/399f68/g;\
	s/3a3b3d/3a3d3b/g;\
	s/3a6fa9/3aa96f/g;\
	s/3a73e2/16a085/g;\
	s/3c3c3e/3c3e3c/g;\
	s/3c3e40/3c403e/g;\
	s/3d3e40/3d403e/g;\
	s/3e5e9d/3e9d5e/g;\
	s/407cbc/16a085/g;\
	s/404042/404240/g;\
	s/4080fb/16a085/g;\
	s/4080FB/16a085/g;\
	s/434446/434644/g;\
	s/4577d6/16a085/g;\
	s/45a3e7/45e7a3/g;\
	s/46484a/464a48/g;\
	s/4a84c1/16a085/g;\
	s/4a86fb/62fbc8/g;\
	s/4B4C4E/4B4E4C/g;\
	s/4b4d50/4b504d/g;\
	s/4c86f3/16a085/g;\
	s/4cb1ff/16a085/g;\
	s/4e86c3/16a085/g;\
	s/4f5053/4f5350/g;\
	s/4f5154/4f5451/g;\
	s/525254/4f5451/g;\
	s/536272/537262/g;\
	s/538dfb/16a085/g;\
	s/548dfb/16a085/g;\
	s/58585a/585a58/g;\
	s/59b5fc/59fcb5/g;\
	s/5A86DB/16a085/g;\
	s/5d5d5f/5d5f5d/g;\
	s/5d5e62/5d625e/g;\
	s/5d93fc/16a085/g;\
	s/5e6064/5e6460/g;\
	s/616163/616361/g;\
	s/616263/616362/g;\
	s/6193c9/16a085/g;\
	s/628cdd/77ddbb/g;\
	s/6496F5/16a085/g;\
	s/656668/656866/g;\
	s/6596ca/16a085/g;\
	s/66bdff/66ffbd/g;\
	s/6a6a6c/6a6c6a/g;\
	s/72a1fc/16a085/g;\
	s/747476/747674/g;\
	s/77a3f6/16a085/g;\
	s/78797a/787a79/g;\
	s/79a6fc/16a085/g;\
	s/7a7a7b/7a7b7a/g;\
	s/7c7d7f/7c7f7d/g;\
	s/7d7e7f/7d7f7e/g;\
	s/868688/868886/g;\
	s/88888b/888b88/g;\
	s/8a8a8b/8a8b8a/g;\
	s/8a8b8c/8a8c8b/g;\
	s/949496/949694/g;\
	s/949596/949695/g;\
	s/949597/949795/g;\
	s/99999b/999b99/g;\
	s/9c9d9e/9c9e9d/g;\
	s/9cbdfd/16a085/g;\
	s/9e9e9f/9e9f9e/g;\
	s/a0c0fd/16a085/g;\
	s/a4c2fd/16a085/g;\
	s/a5a5a7/a5a7a5/g;\
	s/a9a9aa/a9aaa9/g;\
	s/ababad/abadab/g;\
	s/B2BDC4/B2C4BD/g;\
	s/b3ccfd/b3fdde/g;\
	s/b4b4b6/b4b6b4/g;\
	s/B9B9BA/B9BAB9/g;\
	s/bbbbc7/bbc7bb/g;\
	s/c9c9d2/c9d2c9/g;\
	s/d0d0da/d0dad0/g;\
	s/d4e1fb/d4fbe1/g;\
	s/d4e1fb/d4fbe1/g;\
	s/d7d7de/d7ded7/g;\
	s/d9d9e0/d9e0d9/g;\
	s/d9e6fe/d9fee6/g;\
	s/dcdce3/dce3dc/g;\
	s/e2e2e4/e2e4e2/g;\
	s/e4e4e8/e4e8e4/g;\
	s/e5e5e9/e5e9e5/g;\
	s/e7e7ec/e7ece7/g;\
	s/eaeaed/eaedea/g;\
	s/eaeaee/eaeeea/g;\
	s/ebebec/ebeceb/g;\
	s/ebebef/ebefeb/g;\
	s/ecf2ff/ecfff2/g;\
	s/ededf0/edf0ed/g;\
	s/F3F3F5/F3F5F3/g;\
	s/f5f5f7/f5f7f5/g;\
	s/f8f8f9/f8f9f8/g;\
	s/fafafb/fafbfa/g;\
	s/fbfbfc/fbfcfb/g;\
	s/ffffff/f9faf9/g;\
	s/14, 95, 250/14, 250, 95/g;\
	s/22, 22, 23/22, 23, 22/g;\
	s/25, 26, 29/25, 29, 26/g;\
	s/27, 27, 28/27, 28, 27/g;\
	s/29, 29, 31/29, 31, 29/g;\
	s/34, 34, 36/34, 36, 34/g;\
	s/35, 35, 36/35, 36, 35/g;\
	s/36, 36, 38/36, 38, 36/g;\
	s/39, 39, 41/39, 41, 39/g;\
	s/41, 42, 43/41, 43, 42/g;\
	s/49, 49, 51/49, 51, 49/g;\
	s/51, 52, 54/51, 54, 52/g;\
	s/52, 52, 54/52, 54, 52/g;\
	s/53, 54, 56/52, 56, 54/g;\
	s/60, 60, 62/60, 62, 60/g;\
	s/60, 62, 64/60, 64, 62/g;\
	s/64, 124, 188/64, 188, 124/g;\
	s/64, 128, 251/64, 251, 128/g;\
	s/65, 65, 67/65, 67, 65/g;\
	s/70, 70, 72/70, 72, 70/g;\
	s/82, 82, 85/82, 85, 82/g;\
	s/93, 93, 95/93, 95, 93/g;\
	s/100, 150, 245/100, 245, 150/g;\
	s/106, 106, 108/106, 108, 106/g;\
	s/120, 121, 122/120, 122, 121/g;\
	s/148, 149, 150/148, 150, 149/g;\
	s/171, 171, 173/171, 173, 171/g;\
	s/226, 226, 228/226, 228, 226/g;\
	s/243, 243, 245/243, 245, 243/g;\
	s/245, 245, 247/245, 247, 245/g;\
	s/114, 161, 252/114, 252, 161/g;\
	s/151516/151615/g;\
	s/1e1f20/1e201f/g;\
	s/1f1f20/1f201f/g;\
	s/202021/202120/g;\
	s/242426/242624/g;\
	s/262627/262726/g;\
	s/28bd8e/16a085/g;\
	s/2a2a2b/2a2b2a/g;\
	s/2A2A2B/2A2B2A/g;\
	s/2B2B2C/2B2C2B/g;\
	s/2e2e30/2e302e/g;\
	s/2f3032/2f3230/g;\
	s/303031/303130/g;\
	s/303133/303331/g;\
	s/333436/333634/g;\
	s/344463/346353/g;\
	s/353537/353735/g;\
	s/363738/363837/g;\
	s/363739/363937/g;\
	s/37393b/373b39/g;\
	s/37393B/373B39/g;\
	s/37455f/375f52/g;\
	s/3a67bd/16a085/g;\
	s/3a6cce/16a085/g;\
	s/3C3E40/3C403E/g;\
	s/3D5074/3D7463/g;\
	s/3d73dc/16a085/g;\
	s/3e78e6/16a085/g;\
	s/3f3f40/3f403f/g;\
	s/3f4042/3f4240/g;\
	s/3f7cf1/16a085/g;\
	s/414245/414542/g;\
	s/414345/414543/g;\
	s/444547/444745/g;\
	s/454749/454947/g;\
	s/47494A/474A49/g;\
	s/4e4f52/4e524f/g;\
	s/505255/505552/g;\
	s/54565a/545a56/g;\
	s/5991fc/59fcc8/g;\
	s/78787a/787a78/g;\
	s/7F7F8A/7F8A7F/g;\
	s/8bb2fd/8bfdd9/g;\
	s/8f8f91/8f918f/g;\
	s/9abaf8/81f8d0/g;\
	s/a3a3a5/a3a5a3/g;\
	s/A7A7A8/A7A8A7/g;\
	s/a7a7a9/a7a9a7/g;\
	s/bababc/babcba/g;\
	s/cdcdcf/cdcfcd/g;\
	s/d7d7d9/d7d9d7/g" {} \;

cd Vertex-Maia/gtk-2.0
sed -i "s/3c3c3e/3c3e3c/g;\
	s/4080fb/16a085/g;\
	s/454749/454947/g;\
	s/F3F3F5/F3F5F3/g;\
	s/fff\;/f9faf9\;/g;\
	s/FFF\"/F9FAF9\"/g;\
	s/FFFFFF/F9FAF9/g" gtkrc

sed -i "s/232324/232423/g;\
	s/3C3E40/3C403E/g;\
	s/45474A/454A47/g;\
	s/F3F3F5/F3F5F3/g;\
	s/ythickness = 0/xthickness = 1\\n  ythickness = 1/" panel.rc

cd ../../Vertex-Maia-Dark/gtk-2.0
sed -i "s/2B2B2C/2B2C2B/g;\
	s/2c2e2f/2c2f2e/g;\
	s/353638/353836/g;\
	s/3c3c3e/3c3e3c/g;\
	s/4080fb/16a085/g;\
	s/434446/434644/g;\
	s/454749/454947/g;\
	s/F3F3F5/F3F5F3/g;\
	s/ffffff/f9faf9/g;\
	s/FFF\"/F9FAF9\"/g;\
	s/FFFFFF/F9FAF9/g" gtkrc

sed -i "s/232324/232423/g;\
	s/3C3E40/3C403E/g;\
	s/45474A/454A47/g;\
	s/F3F3F5/F3F5F3/g;\
	s/ythickness = 0/xthickness = 1\\n  ythickness = 1/" panel.rc

## add custom items
cd "$TMP/$SRCNAM2-$MANJARO_COMMIT"
cp -r images/* "$PKG/usr/share/themes/Vertex-Maia"
rm -rf $PKG/usr/share/themes/Vertex-Maia-Dark/{gtk-2.0/{Menu,ProgressBar},gtk-3.0/assets,xfwm4}
cp -r images-dark/* "$PKG/usr/share/themes/Vertex-Maia-Dark"
cp Firefox/privatebrowsing-mask.png $PKG/usr/share/themes/Firefox/Vertex-Maia/chrome/images
cp -r Vertex-Maia-Square "$PKG/usr/share/themes"

## add .css for Palemoon
install -dm755 $PKG/usr/share/themes/Palemoon/Vertex-Maia{,-Dark}/chrome
cp Palemoon/userChrome.css $PKG/usr/share/themes/Palemoon/Vertex-Maia/chrome/userChrome.css
cp Palemoon/userChrome-dark.css $PKG/usr/share/themes/Palemoon/Vertex-Maia-Dark/chrome/userChrome.css
ln -s /usr/share/themes/Firefox/Vertex-Maia-Dark/chrome/userContent.css $PKG/usr/share/themes/Palemoon/Vertex-Maia-Dark/chrome/userContent.css
ln -s /usr/share/themes/Firefox/Vertex-Maia/chrome/images $PKG/usr/share/themes/Palemoon/Vertex-Maia/chrome/images
ln -s /usr/share/themes/Firefox/Vertex-Maia-Dark/chrome/images $PKG/usr/share/themes/Palemoon/Vertex-Maia-Dark/chrome/images

## fix corrupt metacity thumbnail
tee $PKG/usr/share/themes/Vertex-Maia{,-Dark}/metacity-1/thumbnail.png < $TMP/$SRCNAM-$VERSION/common/metacity-1/thumbnail.png >/dev/null

## customization for whiskermenu
cd $PKG/usr/share/themes/Vertex-Maia
patch -p1 < $CWD/whiskermenu-rabideau.patch

## add openbox themes
cd $TMP/$SRCNAM2-$MANJARO_COMMIT
cp -r Vertex-Maia{,-Dark} $PKG/usr/share/themes

# Finishing the SlackBuild
cd $TMP/$SRCNAM-$VERSION

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS COPYING HACKING.md README.md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
